package com.ydy.controller.center;

import com.ydy.pojo.Orders;
import com.ydy.pojo.vo.OrderStatusCountsVO;
import com.ydy.service.center.MyOrdersService;
import com.ydy.utils.PageInfo;
import com.ydy.utils.ReturnResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "用户中心我的订单", tags = {"用户中心我的订单相关接口"})
@RestController
@RequestMapping("myOrders")
public class MyOrdersController {


    @Autowired
    private MyOrdersService myOrdersService;

    /**
     * 根据用户id查询订单列表
     * @param userId 用户id
     * @param orderStatus 订单状态
     * @param page 当前页数
     * @param pageSize 当前页条数
     * @return
     */
    @ApiOperation(value = "查询订单列表", notes = "查询订单列表", httpMethod = "POST")
    @PostMapping("/query")
    public ReturnResult query(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "orderStatus", value = "订单状态", required = false)
            @RequestParam Integer orderStatus,
            @ApiParam(name = "page", value = "查询下一页的第几页", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "分页的每一页显示的条数", required = false)
            @RequestParam Integer pageSize) {

        if (StringUtils.isBlank(userId)) {
            return ReturnResult.errorMsg("fuck you");
        }

        PageInfo pageInfo = new PageInfo(page,pageSize);

        List grid = myOrdersService.queryMyOrders(userId,orderStatus,pageInfo);

        pageInfo.setPageResult(grid);
        return ReturnResult.ok(pageInfo);
    }


    /**
     * 商家发货没有后端，所以这个接口仅仅只是用于模拟
     * @param orderId 用户id
     */
    @ApiOperation(value="商家发货", notes="商家发货", httpMethod = "GET")
    @GetMapping("/deliver")
    public ReturnResult deliver(
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @RequestParam String orderId){

        if (StringUtils.isBlank(orderId)) {
            return ReturnResult.errorMsg("订单ID不能为空");
        }
        myOrdersService.updateDeliverOrderStatus(orderId);
        return ReturnResult.ok();
    }


    /**
     * 用户用户确认收货
     * @param orderId 订单id
     * @param userId 用户id
     * @return
     */
    @ApiOperation(value="用户确认收货", notes="用户确认收货", httpMethod = "POST")
    @PostMapping("/confirmReceive")
    public ReturnResult confirmReceive(
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @RequestParam String orderId,
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId){

        Orders orders = myOrdersService.checkUserOrder(userId, orderId);
        if (orders == null ) {
            return ReturnResult.errorMsg("fuck you");
        }

        boolean res = myOrdersService.updateReceiveOrderStatus(orderId);
        if (!res) {
            return ReturnResult.errorMsg("订单确认收货失败！");
        }

        return ReturnResult.ok();
    }

    /**
     * 用户删除订单
     * @param orderId 订单id
     * @param userId 用户id
     * @return
     */
    @ApiOperation(value="用户删除订单", notes="用户删除订单", httpMethod = "POST")
    @PostMapping("/delete")
    public ReturnResult delete(
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @RequestParam String orderId,
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId){

        Orders orders = myOrdersService.checkUserOrder(userId, orderId);
        if (orders == null ) {
            return ReturnResult.errorMsg("fuck you");
        }

        boolean res = myOrdersService.deleteMyOrder(userId, orderId);
        if (!res) {
            return ReturnResult.errorMsg("订单删除失败！");
        }

        return ReturnResult.ok();
    }

    /**
     * 获得订单状态数概况
     * @param userId 用户id
     * @return
     */
    @ApiOperation(value = "获得订单状态数概况", notes = "获得订单状态数概况", httpMethod = "POST")
    @PostMapping("/statusCounts")
    public ReturnResult statusCounts(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId) {

        if (StringUtils.isBlank(userId)) {
            return ReturnResult.errorMsg("fuck you");
        }

        OrderStatusCountsVO result = myOrdersService.getOrderStatusCounts(userId);

        return ReturnResult.ok(result);
    }

    /**
     * 查询订单动向
     * @param userId 用户id
     * @param page 当前页
     * @param pageSize 每页条数
     * @return
     */
    @ApiOperation(value = "查询订单动向", notes = "查询订单动向", httpMethod = "POST")
    @PostMapping("/trend")
    public ReturnResult trend(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "page", value = "查询下一页的第几页", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "分页的每一页显示的条数", required = false)
            @RequestParam Integer pageSize) {

        if (StringUtils.isBlank(userId)) {
            return ReturnResult.errorMsg("fuck you");
        }

        PageInfo pageInfo = new PageInfo(page,pageSize);
        List grid = myOrdersService.getOrdersTrend(userId,pageInfo);

        pageInfo.setPageResult(grid);
        return ReturnResult.ok(pageInfo);
    }
}
